package br.unp.tasi.loja.repositorios.impl;

import br.unp.tasi.loja.repositorios.RepositorioBasico;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(readOnly = true)
public abstract class RepositorioBasicolmpl implements RepositorioBasico {

  @PersistenceContext()
  private EntityManager entityManager;

  /**
   * @see br.unp.tasi.loja.repositorios.RepositorioBasico
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  @Override
  public void gravar(final Object instance) {
    getEntityManager().persist(instance);
  }

  /**
   * @see br.unp.tasi.loja.repositorios.RepositorioBasico
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  @Override
  public Object atualizar(final Object instance) {
    return getEntityManager().merge(instance);
  }

  /**
   * @see br.unp.tasi.loja.repositorios.RepositorioBasico
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  @Override
  public void apagar(Object instance) {
    Object instance2 = getEntityManager().merge(instance);
    getEntityManager().remove(instance2);
  }

  /**
   * @return the entityManager
   */
  protected EntityManager getEntityManager() {
    return entityManager;
  }
}
